package com.cos.hospital.ui.usercenter;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cos.hospital.R;
import com.cos.hospital.code.Code;
import com.cos.hospital.init.BaseActivity;
import com.cos.hospital.model.UHttp;
import com.cos.hospital.model.bean.ErrorBean;
import com.cos.hospital.model.bean.ResultListBean;
import com.cos.hospital.model.bean.user.MyAssisBean;
import com.cos.hospital.model.connector.HttpMsgListener;
import com.cos.hospital.tool.SPUtil;
import com.cos.hospital.tool.UTxt;
import com.cos.hospital.ui.usercenter.adapter.MyAssisAdapter;
import com.cos.hospital.widget.MyDividerDecoration;
import com.cos.hospital.widget.ProgressDialog;
import com.google.gson.JsonObject;
import com.jude.utils.JUtils;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * @Description: 我的助理
 * @Author: Kosmos
 * @Date: 2018年9月13日 20:46
 * @Email: KosmoSakura@foxmail.com
 */
public class AssistantMyActivity extends BaseActivity {
    @BindView(R.id.springView)
    SpringView spv;
    @BindView(R.id.rc)
    RecyclerView rc;
    private List<MyAssisBean> list;
    private BaseQuickAdapter adapter;

    @Override
    protected int layout() {
        return R.layout.activity_my_assistant;
    }

    @Override
    protected void init() {
        setTitleLeftIcon();
        setTitleCenterTxt(getString(R.string.my_assistant));
        setTitleRightTxt(getString(R.string.add_new));

        list = new ArrayList<>();
        adapter = new MyAssisAdapter(list);
        adapter.openLoadAnimation();//开启默认动画AlphaInAnimation
        rc.setLayoutManager(new LinearLayoutManager(context));
        adapter.setEmptyView(R.layout.lay_empty, rc);//设置当list中元素个数为0时显示的布局
        rc.addItemDecoration(new MyDividerDecoration(Color.TRANSPARENT, JUtils.dip2px(1)));
        rc.setAdapter(adapter);
        spv.setHeader(new DefaultHeader(context));
        spv.setFooter(new DefaultFooter(context));
    }

    @Override
    protected void logic() {
        spv.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                refresh();
                spv.onFinishFreshAndLoadDelay();
            }

            @Override
            public void onLoadmore() {
                addMore();
                spv.onFinishFreshAndLoadDelay();
            }
        });
        refresh();
        spv.onFinishFreshAndLoadDelay();
        adapter.setOnItemClickListener((adapter, view, position) -> {
            if (position < 0 || position >= list.size()) {
                showNotice("数据异常");
                return;
            }
            MyAssisBean bean = list.get(position);
            Intent intent = new Intent(context, AssistantReviseActivity.class);
            intent.putExtra(Code.Actions.AssistantId, bean.getFldAssistantId());
            startActivityForResult(intent, 9010);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 9010 && resultCode == 1000) {
            refresh();
        }
    }

    private void refresh() {
        ProgressDialog.getInstance().startProgressDialog(this);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("id", SPUtil.getUserId());
        UHttp.start(getServes().getMyAssist(jsonObject), "我的助理", new HttpMsgListener<ResultListBean<MyAssisBean>>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                addEvent(disposable);
            }

            @Override
            public void onError(String describe) {
                showNotice(describe);
            }

            @Override
            public void onFinish() {
                ProgressDialog.getInstance().stopProgressDialog();
            }

            @Override
            public void onSuccess(ResultListBean<MyAssisBean> dto) {
                if (dto.isSuccess()) {
                    List<MyAssisBean> myAssisBeanList = dto.getResult();
                    list.clear();
                    if (!UTxt.isEmpty(myAssisBeanList)) {
                        list.addAll(myAssisBeanList);
                    }
                    adapter.notifyDataSetChanged();
                    spv.setEnableFooter(true);
                } else {
                    ErrorBean errorBean = dto.getError();
                    if (errorBean != null) {
                        showNotice(UTxt.isNull(errorBean.getMessage(), Code.DefaultPrompt));
                    } else {
                        showNotice(Code.DefaultPrompt);
                    }
                }
            }
        });
    }

    private void addMore() {
        adapter.notifyDataSetChanged();
        spv.setEnableFooter(list.size() < 20);
    }

    /**
     * 1.分行展示已有助理姓名，点击姓名所在行区域进入助理编辑页面；
     * <p>
     * 2.点击新增助理，进入新增助理页面；
     * <p>
     * 3.此页面只展示可用状态的助理列表；
     */
    @OnClick({R.id.title_back, R.id.title_next_txt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.title_next_txt://新增助理:
                startActivityForResult(new Intent(context, AssistantAddActivity.class), 9010);
                break;
        }
    }
}
